package Client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MainClient extends Thread
{
	private static int port = 9909;
	private static String host = "127.0.0.1";
	private Socket sock;
	boolean connect = false;
	@Override
	public void run()
	{
		byte[] buf = new byte [256];
		String str;
		try{
			sock = new Socket(host, port);
			connect = true;
			System.out.println(String.format("Da ket noi voi may chu"));
			InputStream in = sock.getInputStream();
			while(in.read(buf)!= 1)
			{
				str = new String(buf);
				System.out.println(String.format("%s", str));
				if(str.trim().equals("exit"))
					return;
			}
			System.out.println(String.format("TEST %s"));
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public synchronized void send2Server(String msg)
	{
		if (connect == true)
			try{
				OutputStream out = sock.getOutputStream();
				out.write(msg.getBytes());
				out.flush();
			}catch(IOException e)
			{
				e.printStackTrace();
			}
	}
}
